﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Faust.Andrew.MTG.Player
{
	/// <summary>
	/// Interaction logic for AddTokenWindow.xaml
	/// </summary>
	public partial class AddTokenWindow : Window
	{
        public static readonly DependencyProperty PresenterProperty = DependencyProperty.Register("Presenter", typeof(GamePresenter), typeof(AddTokenWindow), new UIPropertyMetadata(null, new PropertyChangedCallback(OnPresenterChanged), new CoerceValueCallback(OnCoercePresenter)));

        private static object OnCoercePresenter(DependencyObject o, object value)
        {
            AddTokenWindow addTokenWindow = o as AddTokenWindow;
            if (addTokenWindow != null)
                return addTokenWindow.OnCoercePresenter((GamePresenter)value);
            else
                return value;
        }

        private static void OnPresenterChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            AddTokenWindow addTokenWindow = o as AddTokenWindow;
            if (addTokenWindow != null)
                addTokenWindow.OnPresenterChanged((GamePresenter)e.OldValue, (GamePresenter)e.NewValue);
        }

        protected virtual GamePresenter OnCoercePresenter(GamePresenter value)
        {
            // TODO: Keep the proposed value within the desired range.
            return value;
        }

        protected virtual void OnPresenterChanged(GamePresenter oldValue, GamePresenter newValue)
        {
            // TODO: Add your property changed side-effects. Descendants can override as well.
        }

        public GamePresenter Presenter
        {
            // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
            get
            {
                return (GamePresenter)GetValue(PresenterProperty);
            }
            set
            {
                SetValue(PresenterProperty, value);
            }
        }

        private Token _token;
        public Token Token
        {
            get { return _token; }
        }

		public AddTokenWindow()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.            
		}

		private void btnCancel_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            DialogResult = false;
            Hide();
		}

		private void btnOK_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            if (!(lbCardImages.SelectedItem is KeyValuePair<string, ImageSource>))
            {
                MessageBox.Show("Must select card image");
                return;
            }

            if (txtModifierText.Text.Length == 0)
            {
                MessageBox.Show("Must enter token description");
                return;
            }

            KeyValuePair<string, ImageSource> value = (KeyValuePair<string, ImageSource>)lbCardImages.SelectedItem;

            _token = new Token()
            {
                ImageKey = value.Key,
                ModifierText = txtModifierText.Text
            };

            DialogResult = true;
            Hide();
		}        

        
	}
}